﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;

namespace DnnDash.DesktopDashboard
{
    public partial class frmSignin : Form
    {
        Authentication credentials = new Authentication();
        public frmSignin()
        {
            InitializeComponent();
            credentials.Cookies = new CookieContainer();
            string httpAlias = Properties.Settings.Default.HttpAlias;
            if (string.IsNullOrEmpty(httpAlias) == false)
                txtHttpAlias.Text = httpAlias;
        }
        internal Authentication Credentials
        {
            get
            {
                return credentials;
            }
        }
        private void btnSignin_Click(object sender, EventArgs e)
        {

            string username = txtUsername.Text;
            string password = txtPassword.Text;
            credentials.Username = username;
            credentials.Password = password;
            credentials.HttpAlias = txtHttpAlias.Text;
            //do sign in
            string url = DnnRequest.GetUrl(credentials.HttpAlias, "DnnDash_SimpleDashboard", "Dash", "PingHost", false);
            string errorMsg = null; HttpStatusCode statusCode;
            CookieContainer cookies = credentials.Cookies;
            string response = DnnRequest.GetRequest(url, username, password, out statusCode, out errorMsg, ref cookies);
            switch (statusCode)
            {
                case HttpStatusCode.Unauthorized:
                case HttpStatusCode.Forbidden:
                    //not authorised
                    MessageBox.Show(errorMsg, "Not Authorised, please check password");
                    break;
                case HttpStatusCode.OK:
                    //open main form
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    Close();
                    break;
                default:
                    //something else entirely!
                    MessageBox.Show(errorMsg + " \n\n Please contact the system administrator for http://" + credentials.HttpAlias, "Http Status: " + statusCode.ToString(), MessageBoxButtons.OK);
                    break;
            }
        }
    }
}
